<!--
Author: W3layouts
Author URL: http://w3layouts.com
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
-->
<!DOCTYPE html>
<html>
<head>
    <title>注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="application/x-javascript"> addEventListener("load", function () {
        setTimeout(hideURLbar, 0);
    }, false);
    function hideURLbar() {
        window.scrollTo(0, 1);
    } </script>
    <link href="../../resources/index/css/font-awesome.min.css" rel="stylesheet" type="text/css" media="all">
    <link href="../../resources/index/css/snow.css" rel="stylesheet" type="text/css" media="all"/>
    <link href="../../resources/index/css/style.css" rel="stylesheet" type="text/css" media="all"/>
    <script style="text/javascript" src="../../resources/index/js/index.js"></script>
</head>
<body>
<div class="snow-container">
    <div class="snow foreground"></div>
    <div class="snow foreground layered"></div>
    <div class="snow middleground"></div>
    <div class="snow middleground layered"></div>
    <div class="snow background"></div>
    <div class="snow background layered"></div>
</div>

<div class="top-buttons-agileinfo">
    <a href="login.html">登录</a><a href="signup.html" class="active">注册</a>
</div>
<h1>注册账号</h1>

<div class="main-agileits">
    <!--form-stars-here-->
    <div class="form-w3-agile">
        <h2 class="sub-agileits-w3layouts">注册</h2>

        <form action="#" method="post" id='submitform' name="frm-reg">
            <input id="email" type="email" name="email" placeholder="用户名（请输入您的邮箱）" required="required"/>
            <input id="password" type="password" name="Password" placeholder="请输入您密码"/>
            <input id="code" type="text" name="Username" placeholder="验证码"/>

            <div class="submit-w3l">
                <input id="get_code" alt=0 type="submit" value="获取验证码">
                <input id="zuce" alt=0 type="submit" value="注册">
            </div>
        </form>
    </div>
</div>
<script style="text/javascript" src="../../resources/lib/jQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#get_code").mouseover(function () {
            $("#get_code").attr('alt', 1);
        });
        $("#get_code").mouseleave(function () {
            $("#get_code").attr('alt', 0);

        });

        $("#zuce").mouseover(function () {
            $("#zuce").attr('alt', 1);
            $("#password").attr('required', 'required');
            $("#code").attr('required', 'required');
        });
        $("#zuce").mouseleave(function () {
            $("#zuce").attr('alt', 0);
            $("#password").removeAttr('required');
            $("#code").removeAttr('required');
        })
    });

    $("#submitform").on("submit", function (ev) {
        var email = $("#email").val();
        var codeAlt = $("#get_code").attr('alt');
        var zuceAlt = $("#zuce").attr('alt');
        if (codeAlt == 1 && zuceAlt == 0) {
            $.ajax({
                type: 'GET',
                url: 'http://www.xiaowenxiong.cn/test/users/getVerificationCode?email=' + email,
                dataType: 'json',
                success: function (result) {
                    if (result.error == 0) {
                        alert("验证码已发送至邮箱，请注意查收!");
                    }
                },
                complete: function () {
                },
                error: function (xhr, type) {

                    alert("请求出错");
                }
            });
        } else if (codeAlt == 0 && zuceAlt == 1) {
            var password = $("#password").val();
            var code = $("#code").val();
            $.ajax({
                type: 'POST',
                url: 'http://xiaowenxiong.cn/test/users/register/email',
                data: {
                    email: email,
                    password: password,
                    verificationCode: code
                },
                dataType: 'json',
                success: function (result) {
                    console.log(result);
                    if (result.error == 103) {
                        alert("此用户以注册！");
                    } else if (result.error == 102 || result.error == 101) {
                        alert("验证码错误！")
                    } else if (result.error == 104) {
                        alert("注册失败，请重试！")
                    } else if (result.error == 0) {
                        window.location.href = "../travel-note/travel-note.html?userId=" + result.data.userId;
                        //存储
                        window.sessionStorage.setItem(loginUserId, result.data.userId);
                    }
                },
                complete: function () {
                },
                error: function (xhr, type) {

                    alert("请求出错");
                }
            });

        }

        //阻止submit表单提交
        ev.preventDefault();
        //或者return false
        //return false;
    });
</script>

</body>
</html>